using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.ComponentModel;
using System.Windows.Data;
using WpfMvvmTemplate.MD.Models.BindingModels;
using WpfMvvmTemplate.MD.Models.EntityModels;
using WpfMvvmTemplate.MD.Views.UserControls;

namespace WpfMvvmTemplate.MD.ViewModels.Main
{
    public partial class MainWindowViewModel : ObservableObject
    {
        /// <summary>
        /// 窗体模型
        /// </summary>
        [ObservableProperty]
        public MainWindowModel? mainWindowModels;

        /// <summary>
        /// 内容
        /// </summary>
        [ObservableProperty]
        public object? content;

        /// <summary>
        /// 选中页索引
        /// </summary>
        [ObservableProperty]
        public int selectedIndex;

        partial void OnSelectedIndexChanged(int value)
        {
            CurrentSelectedIndex = value == -1 ? 0 : value;
        }

        /// <summary>
        /// 当前选中页索引
        /// </summary>
        [ObservableProperty]
        public int currentSelectedIndex;

        /// <summary>
        /// 搜索关键字
        /// </summary>
        [ObservableProperty]
        public string? searchKeyword;

         partial void OnSearchKeywordChanged(string? value)
        {
            demoItemsView.Refresh();
        }

        //刷新搜索关键字
        private readonly ICollectionView demoItemsView;

        public MainWindowViewModel()
        {
            MainWindowModels = new MainWindowModel();

            //测试中英文
            //var a = GlobalStaticData.LanguageDic["hello"];

            //设置搜索关键字
            demoItemsView = CollectionViewSource.GetDefaultView(MainWindowModels.MenuBarColl);
            demoItemsView.Filter = DemoItemsFilter;
        }

        /// <summary>
        /// 窗体加载事件
        /// </summary>
        [RelayCommand]
        public void MainWindowViewLoaded()
        {
            Content = App.Current.Services.GetRequiredService<HomeView>();
        }

        #region 导航行按钮 搜索关键字

        /// <summary>
        /// 上一步
        /// </summary>
        [RelayCommand]
        public void MovePrev()
        {
            if(!string.IsNullOrWhiteSpace(SearchKeyword))
                SearchKeyword = string.Empty;

            SelectedIndex = CurrentSelectedIndex - 1 < 0 ? 0 : CurrentSelectedIndex - 1;
        }

        /// <summary>
        /// 下一步
        /// </summary>
        [RelayCommand]
        public void MoveNext()
        {
            if(!string.IsNullOrWhiteSpace(SearchKeyword))
                SearchKeyword = string.Empty;

            SelectedIndex = Math.Min(CurrentSelectedIndex + 1, MainWindowModels!.MenuBarColl.Count - 1);
        }

        /// <summary>
        /// Home按钮
        /// </summary>
        [RelayCommand]
        public void Home()
        {
            SearchKeyword = string.Empty;
            SelectedIndex = 0;
        }

        /// <summary>
        /// 设置搜索关键字
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private bool DemoItemsFilter(object obj)
        {
            if(string.IsNullOrWhiteSpace(SearchKeyword))
            {
                return true;
            }

            return obj is MenuBar item && item.Title.ToLower().Contains(SearchKeyword!.ToLower());
        }

        #endregion 导航行按钮 搜索关键字

        /// <summary>
        /// 切换页面
        /// </summary>
        [RelayCommand]
        public void ListBoxSelectionChanged(string nameSpace)
        {
            switch(nameSpace)
            {
                case "HomeView": Content = App.Current.Services.GetRequiredService<HomeView>(); break;
                case "ViewA": Content = App.Current.Services.GetRequiredService<ViewA>(); break;
                case "ViewB": Content = App.Current.Services.GetRequiredService<ViewB>(); break;
            }
        }
    }
}